//
//  RegisterViewController.m
//  OrderSeat
//
//  Created by haiming on 13-10-4.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "RegisterViewController.h"
#import "QuickRegisterViewController.h"
#import "LoginViewController.h"
#import "AppSetting.h"

@implementation RegisterViewController

- (id)init
{
    self = [super initWithNibClass:[RegisterViewController class]];
    if (self) {
        _dataSource = [[RegisterDataSource alloc] initWithVC:self];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setUpUI];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[AppDelegate sharedInstance] hideTabBar:self.tabBarController];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[AppDelegate sharedInstance] showTabBar:self.tabBarController];
}

- (void)setUpUI
{
    [self createLeftButton];
    [self createRightButton:@"登录"];
    [self createNavigation:@"会员注册"];
    [self.view addTarget:self action:@selector(viewTapped:)];
    [_contentView drawBorder];
}

- (void)rightButtonClick:(id)sender
{
    [self pushOrPopViewController:[LoginViewController class]];
}

- (IBAction)nextAction:(id)sender
{
    if (![self checkForm]) return;
    
    _userName = _userNameTF.text;
    _password = _passwordTF.text;
    
    NSString *url = [NSString stringWithFormat:API_REGISTER, _userName,_password];
    [_dataSource registerWithURL:url];
}

-(void)viewTapped:(id)sender
{
    [_userNameTF resignFirstResponder];
    [_passwordTF resignFirstResponder];
    [_makeSurePWTF resignFirstResponder];
}

- (BOOL)checkForm
{
    if (_userNameTF.text.length <= 0) {
        [AppDelegate showMessage:@"用户名不能为空"];
        return NO;
    }
    
    if (_passwordTF.text.length <= 0) {
        [AppDelegate showMessage:@"密码不能为空"];
        return NO;
    }
    
    if (![_passwordTF.text isEqualToString:_makeSurePWTF.text]) {
        [AppDelegate showMessage:@"两次输入的密码不一致"];
        return NO;
    }
    
    return YES;
}

@end

//=========================================================================
#pragma ===================数据源类实现======================================
//=========================================================================

@implementation RegisterDataSource

- (id)initWithVC:(id)viewController
{
    self = [super init];
    if(self) {
        self.viewController = viewController;
    }
    return self;
}

- (void)registerWithURL:(NSString *)url
{
    [SVProgressHUD show];
    [self get:url];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    [SVProgressHUD dismiss];
    CLog(@"%@", request.responseString);
    
    NSDictionary *json = [request.responseData JSONValue];
    NSDictionary *data = [json objectForKey:@"data"];
    
    [self processRequest:data];
}

- (void)processRequest:(NSDictionary *)data
{
    int state = [[data objectForKey:@"state"] integerValue];
    if (state == 1) {
        [AppSetting setUserName:_viewController.userName];
        QuickRegisterViewController *quickRegiseter = [[QuickRegisterViewController alloc] initWithType:ValidateTel];
        [_viewController pushViewController:quickRegiseter];
    }
    
    if (state == 2) {
        [SVProgressHUD showErrorWithStatus:@"该会员名称已经存在!"];
    }
}

@end

